package com.hhj.controller;

import com.hhj.dao.DepartmentDao;
import com.hhj.dao.EmployeeDao;
import com.hhj.pojo.Department;
import com.hhj.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private DepartmentDao department;

    @RequestMapping("/golist")
    public String goList(Model model){
        Collection<Employee> allEmployee = employeeDao.getAllEmployee();
        model.addAttribute("employees",allEmployee);
        return "/emp/list";
    }

    @GetMapping("/toAdd")
    public String toAdd(Model model){
        Collection<Department> departments = department.getDepartments();
        model.addAttribute("depart",departments);
        return "/emp/addEmps";
    }
    @PostMapping("/toAdd")
    public String addEmp(Employee employee){
        employeeDao.addEmploy(employee);
        return "redirect:/golist";
    }
    @GetMapping("/toUpdate/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id,Model model){
        Employee employeeById = employeeDao.getEmployeeById(id);
        Collection<Department> departments = department.getDepartments();
        model.addAttribute("depart",departments);
        model.addAttribute("emp",employeeById);
        return "/emp/updateEmp";
    }

    @PostMapping("/toUpdate")
    public String updateEmp(Employee employee){
        employeeDao.addEmploy(employee);
        return "redirect:/golist";
    }

    @GetMapping("/delete/{id}")
    public String deleteEmp(@PathVariable("id")Integer id){
        employeeDao.deleteEmployeeById(id);
        return "redirect:/golist";
    }
}
